/*
 * WidgetRoot.h
 *
 *	Author: stravant
 *	Description:
 *		
 */

#ifndef WIDGETROOT_H_
#define WIDGETROOT_H_

#include "WidgetContainer.h"
#include "../core/Include.h"
#include "../ui/Canvas.h"
#include "../ui/WidgetTraits.h"

namespace tbt {

class Window;

class WidgetRoot: public WidgetContainerAbsolute, public TraitBackgroundColor {
public:
	WidgetRoot();
	~WidgetRoot();

	//mouse-grab, contol which has the mouse captured
	void setMouseGrab(Ref<Widget> widget);
	Ref<Widget> getMouseGrab() const {return mMouseGrab; }

	//key-grab, contol which is recieving keyboard events
	void setKeyGrab(Ref<Widget> widget) {mKeyGrab = widget; }
	Ref<Widget> getKeyGrab() const {return mKeyGrab; }

	//get the rendercontext
	fx::Context* getContext() const;

	//get window
	Ref<Window> getWindow() const;
protected:
	//handle global key events. A true return value indicates
	//that the event should not be passed to the widgets in
	//this tree.
	virtual bool onGlobalKey(const KeyEvent& e) {return false; }

	//handle a close notification
	virtual void onCloseRequest() {}

	//handle a resize
	virtual void onResized(const IVec& newSize);

	//OS repaint request
	virtual void onRepaint(const IRect& region);

	//event handlers
	void onEvent_Button(const MouseButtonEvent& e);
	void onEvent_Key(const KeyEvent& e);
	void onEvent_Scroll(const MouseWheelEvent& e);
	void onEvent_Crossing(const MouseEvent& e);
	void onEvent_Motion(const MouseEvent& e);

	void onPaint(const IRect& region, fx::Context* ctx);
private:
	static const int kCornerRadius = 5;

	friend class Window;
	Window* mWindow;

	fx::Color mBackgroundColor;

	Ref<Widget> mKeyGrab;
	Ref<Widget> mMouseGrab;
	Ref<Widget> mPrevTarget; //control the mouse was over previously
};

}

#endif /* WIDGETROOT_H_ */
